define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            console.log(Config.category_Id);

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/category/index' + location.search,
                    add_url: 'goods/category/add?category_Id=' + Config.category_Id,
                    edit_url: 'goods/category/edit',
                    del_url: 'goods/category/del',
                    multi_url: 'goods/category/multi',
                    import_url: 'goods/category/import',
                    dragsort_url: 'ajax/weigh',
                    table: 'goods_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                escape: false,
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                pagination: false,
                commonSearch: true,
                fixedColumns: true,
                searchFormVisible: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'name', title: __('Name'), operate: 'LEFT'},
                        {field: 'url_image', title: __('Url_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'pid', title: __('Pid'),operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'add',
                                    title: function (row){
                                        return row.name;
                                    },
                                    text: __('查看下一级'),
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    icon: 'fa fa-plus',
                                    url : 'goods/category/index?ids={ids}',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    visible: function (data){
                                        console.log(data);
                                        if (data.level < 3)
                                        {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: 'del',
                                    text: __('删除'),
                                    title: __('删除'),
                                    confirm: '您确定删除吗',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    icon: 'fa fa-close',
                                    url: $.fn.bootstrapTable.defaults.extend.del_url,
                                    visible: true,
                                    success: function (data, ret){
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret){
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();


        },
        edit: function () {
            Controller.api.bindevent();

        },
        api: {
            formatter: {
                subnode: function (value, row, index) {
                    // console.log(row)
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('切换菜单') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" data-pids="' + row.pids + '" class="btn btn-xs '
                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
